Ext.ns("CRM.SPComp");

(function(){ 
   
	var issueWin;
	
   CRM.SPComp.showIssueWindow = function (projectId,issueId,rowdata){  
	      var issueCategoryStore  = CRM.commons.createComboStore('issueCategoryStoreid','issue/issueReqCombo','issueCategory');
		  var issueStatusStore    = CRM.commons.createComboStore('issueStatusStoreid','issue/issueReqCombo','issueStatus');
		  var issuePriorityStore  = CRM.commons.createComboStore('issuepriorityStoreid','issue/issueReqCombo','issuePriority');
		  var assignedToStore      =CRM.commons.createComboStore('assignedToStoreid','issue/issueAssignedTo?projectId='+projectId,'');
		  var issueComplexityStore = CRM.commons.createComboStore('issueComplextityStoreid','issue/issueReqCombo','issueComplexity');
		  var issueResolutionStore = CRM.commons.createComboStore('issueResolutionStoreid','issue/issueReqCombo','issueResolution');
		  var memberStore = new Ext.data.JsonStore({
				id		: 'memberStore',
				root    : 'data',
				fields  : [
								{
							    name    : 'id',
							    mapping : 'id'
								},
								{
								name    : 'criteria',
								mapping : 'value'
								}
						],
				proxy 	: new Ext.data.HttpProxy({
				method	: 'GET',
				url	  	: 'issue/memberListByProject?projectId='+projectId
				}),
				remoteSort : false
			}); 
		  
		   issuePanelId ={
				  xtype       : 'fieldset',
				  id          : 'issuePanelId', 
				  frame       : false,
				  border      : false,
				  width       :'590',
	              height      :360,
				  items       :[
				                    {
				                    	xtype       : 'hidden',
				                    	id          : 'issueProjectId',
				                    	fieldLabel  : 'Issue ProjectId'
				                    },
									{
									 	xtype       : 'hidden',
									 	id          : 'issueId',
									 	fieldLabel  : 'Issue Id'
									},
					                {
				                	    xtype        : 'textfield',
				                	    id           : 'issueName',
				                	    fieldLabel   : 'Issue Name',
				                	    width        : '421'
				                	
				                   },
				                  {
				                	 xtype         : 'textarea',
				                	 id            : 'issueReason',
				                     fieldLabel    : 'Issue Reason',
				                     align         : 'top',
				                	 width         :  '421',
				                	 height        :  70
				                  },
				                  {
				                	   xtype       : 'compositefield',
				                	   fieldLabel  : 'Category',
				                	   items       : [
				                	                   {
				                	                	   xtype         : 'combo',
				                	                	   id            : 'category',
				                	                	   value         : 'SELECT',
				                	                	   width         : '170',
				                	                	   store         : issueCategoryStore ,
					     		   		     		   	   displayField  : 'name',
					     		   		     		   	   valueField    : 'id',
					     		   		     		   	   triggerAction : 'all',
					     		   		     		   	   typeAhead     : false,
					     		   		     		       editable	     : false,
					     		   		     		       listeners      :{select:function(){
          	                	                                             bindValue('category','categoryId');
          	                                                                 }
          	                                          }
					     		   		     	        },
				                	                  {
				                	                	    xtype :'hidden',
				                	                	    id    :'categoryId',
				                	                	    width	: 30,
					                	                	 value  :'0'
				                	                  },
				                	                  { xtype :'displayfield', value:'Module Id:', width:'90'},
			            	                          {
			            	                        	   xtype  : 'textfield',
			            	                        	   id     : 'issueModule', 
			            	                        	   width  :'156.5'
			            	                           }
				                	                   
				                	                 ]
				                  },
				                 {
				                	   xtype      :'compositefield',
				                	   fieldLabel : 'Status',
				                	   items      : [
				                	                 
				                	                  {
				                	                	    xtype          : 'combo',
				                	                	    id             : 'status',
				                	                	    value          : 'SELECT',
				                	                	    width          : '170',
				                	                	    store          : issueStatusStore,
					     		   		     		   	 	displayField   : 'name',
					     		   		     		   	 	valueField     : 'id',
					     		   		     		   	 	triggerAction  : 'all',
					     		   		     		   	 	typeAhead      :false,
					     		   		     		         editable	   : false,
					     		   		     		         listeners      :{select:function(){
				                	                	                             bindValue('status','statusId');
				                	                                                     }
				                	                                          }
					     		   		     		         
					     		   		     		    },
					     		   		     		    {
					                	                	 xtype  :'hidden',
					                	                	 id     :'statusId',
					                	                	 width	: 30,
					                	                	 value  :'0'
					                	                 },
					     		   		     		    
				                	                   { xtype :'displayfield', value :'Priority:',width:'90'},
				                	                   {
				                	                	    xtype         :'combo',
				                	                	    id            :'priority',
				                	                	    value         : 'SELECT',
				                	                	    store         : issuePriorityStore,
					     		   		     		    	displayField  : 'name',
					     		   		     		    	valueField    : 'id',
					     		   		     		    	triggerAction : 'all',
					     		   		     		    	typeAhead     :false,
					     		   		     		    	width		  : 165.5,
					     		   		     		        editable	  : false,
					     		   		     		        listeners      :{select:function(){
				                	                	                       bindValue('priority','priorityId');
				                	                                         }
				                	                                        }
					     		   		     		        
					     		   		                 },
					     		   		                 {
						     		   		     		    	xtype  : 'hidden',
						     		   		     		    	id     :'priorityId',
						     		   		     		        width	: 30,
					                	                	    value  :'0'
						     		   		     		    },
					     		   		                 
				                	                 ]
				                },
				               {
			            	       xtype        : 'compositefield',
			            	       fieldLabel   : 'AssignedTo',
			            	       items        : [
			            	                       
			            	                         {
			            	                        	    xtype         : 'combo',
			            	                        	    id            : 'assignedTo',
			            	                        	    value         : 'SELECT',
			            	                        	    width         : '170',
			            	                        	    store         : memberStore,
					     		   		     		    	displayField  : 'criteria',
					     		   		     		    	valueField    : 'id',
					     		   		     		    	triggerAction : 'all',
					     		   		     		    	typeAhead     : false,
					     		   		     		        editable	  : false,
					     		   		     		        listeners     :{select:function(){
			            	                    	                         bindValue('assignedTo','assignedToId');
			            	                                               }
			            	                                              }
					     		   		     		       
			            	                          },
			            	                          {
				            	                    	   xtype : 'hidden',
				            	                    	   id    : 'assignedToId',
				            	                    	   value :'0'
				            	                       }, 
			            	                          
			            	                          { xtype :'displayfield', value:'Complexity:', width:'90'},
			            	                          {
			            	                        	   xtype         : 'combo',
			            	                        	   id            : 'complexity',
			            	                        	   value         : 'SELECT',
			            	                        	   store         : issueComplexityStore,
					     		   		     		   	   displayField  : 'name',
					     		   		     		   	   valueField    : 'id',
					     		   		     		   	   triggerAction : 'all',
					     		   		     		   	   typeAhead     : false,
					     		   		     		   	   width		 : 165.5,
					     		   		     		        editable	 : false,
					     		   		     		       listeners     :{select:function(){
	      	                    	                                           bindValue('complexity','complexityId');
	      	                                                               }
	      	                                                           }
					     		   		     		        
			            	                          },
			            	                          {
			            	                        	  xtype :'hidden',
			            	                        	  id    :'complexityId',
			            	                        	  value :'0'
			            	                          }
			            	                        ]
			                       }, 
			                       {
		                        	   xtype         : 'combo',
		                        	   fieldLabel    : 'Resolution',
		                        	   id            : 'resolution',
		                        	   value         : 'SELECT',
		                        	   store         : issueResolutionStore,
			   		     		   	   displayField  : 'name',
			   		     		   	   valueField    : 'id',
			   		     		   	   triggerAction : 'all',
			   		     		   	   typeAhead     : false,
			   		     		   	   width		 : 165, 
			   		     		        editable	 : false,
			   		     		       listeners     :{select:function(){
	      	                                           bindValue('resolution','resolutionId');
	                                                 }
	                                             }
			   		     		        
		                          }, 
		                          {
		                        	  xtype :'hidden',
		                        	  id    :'resolutionId',
		                        	  value :'0'
		                          },
			                       {
									xtype		: 'compositefield',
									fieldLabel	: 'Target Date',
									items		: [
									     		    {
									     		    	xtype		: 'datefield',
									     		   	 	id			: 'targetDate',
									     		   	 	width		: 165,
									     		   	 	format		: 'Y-m-d',
									     		   	     value		: '0'
									     		    },
									     		    {xtype : 'displayfield', value: 'Date Resolved: ', width:'90'},
									     		    {
									     		    	xtype		: 'datefield',
									     		    	id			: 'dateResolved',
									     		   	 	width		: 165.5,
									     		   	 	format		: 'Y-m-d',
									     		    	value		: '0'
									     		    }
									     		   ]
			                 }
			                
			    			
				 ]
				 };
		   
		   var issueRootPanel ={
	               xtype       : 'fieldset',
	               id          : 'issueRootPanel',
	               height      :  300,
	               width       :  570,
	               frame       : true,
	               items       : [
	                               {
	                                   xtype   : 'panel',
	                                   layout  : 'hbox',
	                                   items   : [issuePanelId]
	                               }
	                             ],
	               bbar        : {
	                               xtype       : 'toolbar',
	                               buttonAlign : 'right',
	                               items       : [
	                                              
													/*{
													    xtype  : 'button',
													    text   : 'Create New',
													    id     : 'newButton',
													    handler: newIssueForm
													    
													 },'-',*/
		    	                                      {
			    	                                    	  xtype  :'button',
			    	                                    	  text   :'Save',
			    	                                    	  id     :'saveIssueButton',
			    	                                    	  handler : function(){
		    	                                    	       var actionValue = Ext.getCmp('saveIssueButton').getText();
		    	                                    	       addIssues(actionValue); 
			    	                                      				}
		    	                                      },'-',
		    	                                      {
		    	                                    	      xtype   : 'button',
		    	                                    	      text    : 'Cancel',
		    	                                    	      id      :'cancelButton',
		    	                                    	      handler : closeIssueWindow
		    	                                      } 
	    	                                      
	                                           ]
	                             }
	           };
		   
		   var issueMainPanel = new Ext.FormPanel({
				  id      : 'issueMainPanel',
				  frame   : true,
				  height  : 410,
				  width   : 590,
				  items   : [issueRootPanel]
				  
			  }) ; 
		
	 
		 
			   issuesWin =new Ext.Window({
		       title        :'Create/Update Issue',
		       iconCls      :'Issue',
		       id           :'issueWindow',
		       width        : 595,
		       height       : 340,
		       border       : true,
		       plain        : false,
		       closable    : true,
		       collapsible  : true, 
		       animCollapse : true,
		       draggable    : true,
		       onEsc        : true,
		       model        : true,
		       items        :[issueMainPanel]
		      
		  });
			   
			   if(rowdata!=null){
			   Ext.getCmp('saveIssueButton').setText('Update');
               	showIssueDetails(rowdata,issueMainPanel);
			   }
		      issuesWin.show(this);
		      var proId = Ext.getCmp('projectId');
		      var issueProId = Ext.getCmp('issueProjectId');
		      issueProId.setValue(proId.getValue());
		      return issueWin;
   
		      
   };
  
   

  // Save Issue Function
   
   function addIssues(action) {
	   var formPanel = Ext.getCmp('issueMainPanel');
	   var message = 'No action taken.';
	   var  test;
	   if(action == 'Save'){
	  	  	 message = 'Saved Successfully!'; 
	  	  	test=undefined;
	   }
	   if(action == 'Update'){ 
		  test=formPanel.getForm().findField('issueId').value;
	  	  	 message = 'Updated Successfully!'; 
	    } 
	   if(formPanel.getForm().findField('issueId').value==test){
	   var values = formPanel.getForm().getValues();
	   var encoded = Ext.encode(values);
	   Ext.Ajax.request({
	        		   url		: 'issue/save?action='+action,
	        		   method	: 'POST',
	      	            success : function(request) { 
	  	        		       alert("Issue "+action+"d Successfully. "+request.responseText);
	  	        		       formPanel.getForm().findField('issueId').value=request.responseText;
	  	        		       var issueGrd = Ext.getCmp('issueGrid');
	  	        		       issueGrd.store.removeAll();
	  	        		       issueGrd.store.reload({
	  	        		        			params: {
	  	        		        						issueProjectId: formPanel.getForm().findField('issueProjectId').value  
	  	        									}
	  	        			  });
	  	        		 },
	      	   failure: function() { alert ("Failure"); },
	      	   jsonData: encoded  
	      	});
	   }
	   else
		   alert("Record Already Exist");
		  };
	  
	  
      // fuction for display value in add comment
	  function showIssueDetails(rowdata,issueMainPanel){

	   		var projectgrid = Ext.getCmp('spSearchProjectGridPanel');
	   		if(projectgrid!= null)
	   		{
	   			  var projectId = Ext.getCmp('projectId').getValue();
	   			  var issueProjectId = Ext.getCmp('issueProjectId').setValue(projectId);
	   		}
	   		issueMainPanel.getForm().loadRecord(rowdata);
	   };
   
// Create New Function

function newIssueForm(){ 
	
	issueMainPanel.getForm().reset();
};

function disableIssueForm(){
	
	Ext.getCmp('issueName').disable();
	Ext.getCmp('issueReason').disable();
	Ext.getCmp('issueModule').disable();
	Ext.getCmp('issueCategory').disable();
	Ext.getCmp('issueStatus').disable();
	Ext.getCmp('issuePriority').disable();
	Ext.getCmp('issueAssignedTo').disable();
	Ext.getCmp('issueComplexity').disable();
	Ext.getCmp('issueResolution').disable();
	Ext.getCmp('targetDate').disable();
	Ext.getCmp('dateResolved').disable();
	Ext.getCmp('saveButton').disable(); 
	Ext.getCmp('editButton').enable();
};

function closeIssueWindow(){
   	issuesWin.close();
   	
   }

})();


